package com.igeek.d0613.test1;

import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @Author: zhiyu
 * @Date:16:49
 * @DESC:
 */
public class ProductThread extends Thread{
    private List<Object> num;
    private Thread consumer;

    public ProductThread(String name, List<Object> num) {
        super(name);
        this.num = num;
    }

    public void setConsumer(Thread consumer) {
        this.consumer = consumer;
    }
    @Override
    public void run() {
        for (int i = 0; i < 15; i++) {
            while(num.size()>=10){
                //调用Thread父类的getName 获取线程的名字
                System.out.println(super.getName()+"仓库已满...");
                //阻塞当前线程
                LockSupport.park();
            }
            //进入生产
            num.add(new Object());
            System.out.println(super.getName()+"生产了："+num.size());
            //唤醒消费者
            LockSupport.unpark(consumer);
        }
    }
}
